package com.cbh.domain.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cbh.common.id.IdWorker;
import com.cbh.entity.Area;
import com.cbh.entity.InsuranceCompany;
import com.cbh.entity.OfferInterface;
import com.cbh.entity.enums.State;
import com.cbh.mapper.OfferInterfaceMapper;

@Service
public class OfferInterfaceService {
	private static final Log logger = LogFactory.getLog(OfferInterfaceService.class);

	@Autowired
	OfferInterfaceMapper oiMapper;
	@Autowired
	InsuranceCompanyService icService;
	@Autowired
	AreaService areaService;

	/**
	 * 保存报价接口信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	public String save(OfferInterface ob) throws Exception {
		if (null == ob) {
			throw new Exception("数据对象为空");
		}
		if (StringUtils.isEmpty(ob.getOiId())) {
			return insert(ob);
		}
		return update(ob);
	}

	/**
	 * 保存报价接口信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	public String insert(OfferInterface ob) throws Exception {
		if (null == ob) {
			throw new Exception("数据对象为空");
		}
		ob.setOiId(IdWorker.getId().toString());
		if (null == ob.getOiName()) {
			throw new Exception("请传入接口名称");
		}
		if (null == ob.getOiType()) {
			ob.setOiType("crawler");
		}
		if (null == ob.getOi_icId()) {
			throw new Exception("请传入保险公司序号");
		}
		InsuranceCompany ic = icService.getOne(ob.getOi_icId());
		if (null == ic) {
			throw new Exception("请传入有效的保险公司");
		}
		ob.setOi_icName(ic.getIcName());
		if (null == ob.getOi_areaId()) {
			throw new Exception("请传入所属区域");
		}
		Area area = areaService.getOne(ob.getOi_areaId());
		if (null == area) {
			throw new Exception("请传入有效的所属区域");
		}
		if (null == ob.getOiPathPremiumCalculation()) {
			throw new Exception("请传入报价接口");
		}
		if (ob.getOiType().equals("directly")) {
			if (null == ob.getOiPathConfirm()) {
				throw new Exception("请传入投保确认地址");
			}
			if (null == ob.getOiPathConfirm()) {
				throw new Exception("请传入支付地址");
			}
		}

		ob.setOi_areaName(area.getAreaName());
		ob.setOiState(State.EFFECTIVE.getCode());

		try {
			Integer re = oiMapper.insert(ob);
			if (re > 0) {
				return ob.getOiId();
			} else {
				throw new Exception("保存数据失败");
			}
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("保存数据失败");
		}
	}

	/**
	 * 保存报价接口信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	public String update(OfferInterface ob) throws Exception {
		if (null == ob) {
			throw new Exception("数据对象为空");
		}
		if (null != ob.getOiType() && ob.getOiType().equals("directly")) {
			if (null == ob.getOiPathConfirm()) {
				throw new Exception("请传入投保确认地址");
			}
			if (null == ob.getOiPathConfirm()) {
				throw new Exception("请传入支付地址");
			}
		}
		try {
			Integer re = oiMapper.update(ob);
			if (re > 0) {
				return ob.getOiId();
			} else {
				throw new Exception("更新数据失败");
			}
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("更新数据失败");
		}
	}

	/**
	 * 真删除报价接口
	 * 
	 * @param proId
	 * @return
	 * @throws Exception
	 */
	public boolean delete(String id) throws Exception {
		OfferInterface ob = new OfferInterface().id(id);
		try {
			Integer re = oiMapper.delect(ob);
			return re == 1;
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("删除数据失败");
		}
	}

	/**
	 * 禁用
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public boolean disable(String id) throws Exception {
		OfferInterface ob = new OfferInterface().id(id).state(State.INVABLE.getCode());
		try {
			Integer re = oiMapper.update(ob);
			return re == 1;
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("删除数据失败");
		}
	}

	/**
	 * 根据proId获取报价接口信息
	 * 
	 * @param proId
	 * @return
	 * @throws Exception
	 */
	public OfferInterface getOne(String id) throws Exception {
		if (null == id) {
			throw new Exception("主键为空");
		}
		OfferInterface ob = new OfferInterface().id(id);
		try {
			ob = oiMapper.queryOne(ob);
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("获取数据失败");
		}
		return ob;
	}

	/**
	 * 获取全部有效的报价接口
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<OfferInterface> getAll() throws Exception {
		List<OfferInterface> obs = null;
		try {
			obs = oiMapper.queryAll(new OfferInterface().state(State.EFFECTIVE.getCode()));
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("获取数据失败");
		}
		return obs;
	}
}
